package com.vadmin.vadminjava.servlet;

import com.alibaba.fastjson.JSONObject;
import com.vadmin.vadminjava.aes.AesException;
import com.vadmin.vadminjava.service.WxWorkNotifyCustomerService;
import com.vadmin.vadminjava.service.WxWorkNotifyWorkerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;


/* ***********************************************************
 *
 * 企业微信的通知接收接口
 *
 * **********************************************************/

@RestController
@RequestMapping ("/WxWorkNotifyServlet")
public class WxWorkNotifyServlet {

	@Autowired
	WxWorkNotifyWorkerService wxWorkNotifyWorkerService;
	@Autowired
	WxWorkNotifyCustomerService wxWorkNotifyCustomerService;



	/**
	 * 外部客户变更回调接收
	 * @param params
	 * @param req
	 * @return
	 * @throws AesException
	 * @throws IOException
	 */
	@RequestMapping(value={"/ExternalUserChange"})
	public String ExternalUserChange(@RequestParam Map<String, Object> params, HttpServletRequest req) throws AesException, IOException {
		//参数转jsonobject
		String ret = "";
		JSONObject paramJ = new JSONObject();
		params.keySet().forEach(i -> paramJ.put(i, params.get(i)));
		//读取post流
		String str = null;
		StringBuilder stringBuilder = new StringBuilder();
		BufferedReader bufferedInputStream = new BufferedReader(new InputStreamReader(req.getInputStream()));
		try {
			while ((str = bufferedInputStream.readLine()) != null) stringBuilder.append(str);
		} finally {
			bufferedInputStream.close();
		}
		String sReqData      = stringBuilder.toString();//这个应该是整个post流，不是表单模式
		System.out.println("接收到了客户变更正式回调>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		ret =  wxWorkNotifyCustomerService.ExternalUserChange(paramJ,sReqData);//这个是正式的，有签名解密的

		return ret;
	}

	//外部客户变更回调dev，传的直接是解密后的xml
	@RequestMapping(value={"/ExternalUserChangeDev"})
	public String ExternalUserChangeDEV(@RequestParam Map<String, Object> params, HttpServletRequest req) throws AesException, IOException {
		//参数转jsonobject
		String ret = "";
		JSONObject paramJ = new JSONObject();
		params.keySet().forEach(i -> paramJ.put(i, params.get(i)));
		//读取post流
		String str = null;
		StringBuilder stringBuilder = new StringBuilder();
		BufferedReader bufferedInputStream = new BufferedReader(new InputStreamReader(req.getInputStream()));
		try {
			while ((str = bufferedInputStream.readLine()) != null) stringBuilder.append(str);
		} finally {
			bufferedInputStream.close();
		}
		String sReqData      = stringBuilder.toString();//这个应该是整个post流，不是表单模式
		System.out.println("接收到了客户变更回调dev模式>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		ret =  wxWorkNotifyCustomerService.ExternalUserChange2(sReqData);//这个是开发的，发送已解密的xml

		return ret;
	}

	/**
	 * 员工资料变更事件回调
	 * @param params
	 * @param req
	 * @return
	 * @throws AesException
	 * @throws IOException
	 */
	@RequestMapping(value={"/WorkerUserChange"})
	public String WorkerUserChange(@RequestParam Map<String, Object> params, HttpServletRequest req) throws AesException, IOException {
		//参数转jsonobject
		String ret = "";
		JSONObject paramJ = new JSONObject();
		params.keySet().forEach(i -> paramJ.put(i, params.get(i)));
		//读取post流
		String str = null;
		StringBuilder stringBuilder = new StringBuilder();
		BufferedReader bufferedInputStream = new BufferedReader(new InputStreamReader(req.getInputStream()));
		try {
			while ((str = bufferedInputStream.readLine()) != null) stringBuilder.append(str);
		} finally {
			bufferedInputStream.close();
		}
		String sReqData      = stringBuilder.toString();//这个应该是整个post流，不是表单模式
		System.out.println("接收到了员工变更正式回调>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		ret =  wxWorkNotifyWorkerService.WorkerUserChange(paramJ,sReqData);//这个是正式的，有签名解密的

		return ret;
	}

	//员工变更回调dev 传的直接是解密后的xml
	@RequestMapping(value={"/WorkerUserChangeDev"})
	public String WorkerUserChangeDev(@RequestParam Map<String, Object> params, HttpServletRequest req) throws AesException, IOException {
		//参数转jsonobject
		String ret = "";
		JSONObject paramJ = new JSONObject();
		params.keySet().forEach(i -> paramJ.put(i, params.get(i)));
		//读取post流
		String str = null;
		StringBuilder stringBuilder = new StringBuilder();
		BufferedReader bufferedInputStream = new BufferedReader(new InputStreamReader(req.getInputStream()));
		try {
			while ((str = bufferedInputStream.readLine()) != null) stringBuilder.append(str);
		} finally {
			bufferedInputStream.close();
		}
		String sReqData      = stringBuilder.toString();//这个应该是整个post流，不是表单模式
		System.out.println("接收到了员工变更正式回调>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		ret =  wxWorkNotifyWorkerService.WorkerUserChangeDev(paramJ,sReqData);//这个是正式的，有签名解密的

		return ret;
	}
}
